﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Facebook;
using System.Net;
using System.IO;
using System.Threading;
using System.Windows.Threading;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using FBApp.Classes;

namespace FBApp
{
	/// <summary>
	/// Interaction logic for NewsFeed.xaml
	/// </summary>
	public partial class NewsFeed : UserControl
	{        
        private LoadingIndicator indicator;
        private FacebookClient fbc = null;
      
        //Feeds
        public ObservableCollection<NewsFeedItem> newsFeed { get; set; }
        public static Dictionary<string, JSONObject> newsFeedDictionary = new Dictionary<string, JSONObject>();

		public NewsFeed()
		{
			this.InitializeComponent();
            newsFeed = new ObservableCollection<NewsFeedItem>();
            icItems.ItemsSource = newsFeed;            
		}

        public void setIndicator(LoadingIndicator indicator)
        {
            this.indicator = indicator;
        }

        public void setFacebookClient(FacebookClient fbc)
        {
            this.fbc = fbc;            
        }

        public List<JSONObject> updateNewsFeed()
        {
            JSONObject response = fbc.getFacebookAPI().Get("/me/home");

            Dictionary<string, JSONObject> dictionary = response.Dictionary;

            JSONObject[] newsFeedItems = dictionary["data"].Array;

            List<JSONObject> newItems = new List<JSONObject>();

            for (int k = 0; k < newsFeedItems.Length; k++)
            {
                if (!newsFeedDictionary.ContainsKey(newsFeedItems[k].Dictionary["id"].ToDisplayableString()))
                {
                    newsFeedDictionary[newsFeedItems[k].Dictionary["id"].ToDisplayableString()] = newsFeedItems[k];
                    newItems.Add(newsFeedItems[k]);
                }
            }

          //  fbc.getNotifier().displayNotification(newItems.Count + " news feed items added");            
            return newItems;
        }

        public ObservableCollection<NewsFeedItem> getNewsFeed()
        {
            return newsFeed;
        }

        public void addNewsFeedItem(JSONObject item)
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
            {
                if (!item.Dictionary["from"].Dictionary["id"].ToDisplayableString().Equals(fbc.getProfileID()))
                {
                    string message = "";
                    string attrib = "";

                    NewsFeedItem newsFeedItem;

                    if (item.Dictionary.Keys.Contains("message"))
                    {
                        message = item.Dictionary["message"].ToDisplayableString();
                    }
                    else
                    {
                        message = "";
                    }

                    if (item.Dictionary.Keys.Contains("attribution"))
                    {
                        attrib = item.Dictionary["attribution"].ToDisplayableString();
                    }
                    else
                    {
                        attrib = "";
                    }

                    newsFeedItem = new NewsFeedItem(item.Dictionary["id"].ToDisplayableString(), item.Dictionary["from"].Dictionary["name"].ToDisplayableString(), item.Dictionary["from"].Dictionary["id"].ToDisplayableString(), message, item.Dictionary["created_time"].ToDisplayableString(), item.Dictionary["type"].ToDisplayableString(), attrib);

                    if (item.Dictionary.Keys.Contains("link"))
                    {
                        string picture = "";
                        string name = "";
                        string description = "";

                        if (item.Dictionary.Keys.Contains("picture"))
                        {
                            picture = item.Dictionary["picture"].ToDisplayableString();
                        }

                        if (item.Dictionary.Keys.Contains("name"))
                        {
                            name = item.Dictionary["name"].ToDisplayableString();
                        }

                        if (item.Dictionary.Keys.Contains("description"))
                        {
                            description = item.Dictionary["description"].ToDisplayableString();
                        }

                        newsFeedItem.addLink(name, picture, description);
                    }

                    if (item.Dictionary.ContainsKey("comments"))
                    {
                        if ((item.Dictionary["comments"].Dictionary.ContainsKey("data")))
                        {
                            JSONObject[] list = item.Dictionary["comments"].Dictionary["data"].Array;
                            for (int k = 0; k < list.Length; k++)
                            {                                
                                CommentItem ci = new CommentItem(list[k].Dictionary["id"].ToDisplayableString(), list[k].Dictionary["from"].Dictionary["name"].ToDisplayableString(), list[k].Dictionary["from"].Dictionary["id"].ToDisplayableString(), list[k].Dictionary["message"].ToDisplayableString(), list[k].Dictionary["created_time"].ToDisplayableString());
                                newsFeedItem.addComment(ci);
                            }
                        }                         
                    }

                    lock (newsFeed)
                    {
                        newsFeed.Add(newsFeedItem);
                    }
                }
            }
                            ));
        }

        public void refreshNewsFeed()
        {
            List<JSONObject> tempFeed;

            tempFeed = updateNewsFeed();
            for (int k = 0; k < tempFeed.Count; k++)
            {
                addNewsFeedItem(tempFeed[k]);
            }
        }

        public int getCount()
        {
            return newsFeed.Count;
        }

        public void refreshUI()
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
             {
                 icItems.UpdateLayout();                 
             }
             ));
        }
	
	}
}